<html>
<style>
.center {
  margin-left: auto;
  margin-right: auto;
  font-size: 32px;
}
p {text-align: center;}
</style>

<?php

include 'functions.php';
include 'config.php';
$ID=$_GET["ID"];
$ShowUser=$_GET["User"];

if($ID!="")
{
$sql = "SELECT Name, Text, Options, Image FROM stor WHERE RefID=?";
$stmt = $conn->prepare($sql); 
$stmt->bind_param("i", $ID);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) 
  {
  // output data of each row
  while($row = $result->fetch_assoc()) 
	 {
		echo '<table class="center">
		<tr>
        <th>' . applyBB($row["Name"]) . ' </th>
		</tr>';
		if($row["Image"]!="0")
		{
		$imLink="images/0.jpg";
		$sql3 = "SELECT Link, Unlocked, Alt FROM Images WHERE ID=?";
		$stmt = $conn->prepare($sql3); 
		$into=$row["Image"];
		$stmt->bind_param("i", $into);
		$stmt->execute();
		$result3 = $stmt->get_result();
		$imDes="";
		while($row2 = $result3->fetch_assoc()) 
				{
				 if($row2["Unlocked"]=='1')
				 {
				 $imLink=$row2["Link"];
				 $imDes=$row2["Alt"];
				 }
			    }
		echo '<tr>
		<td><p></align><img src="' . $imLink .  '" alt="'. htmlentities($imDes) .'" style="width:400px;"></p></td>
		</tr>';
		}
		echo '<tr>
		<td>' . nl2br(applyBB($row["Text"])) . '</td>
		</tr>
		<tr>
		<td></td>
		</tr>';
		$Options=$row["Options"];
		if($Options!="")
		{
		$choices= explode(" ", $Options);
		$arrLength = count($choices);
		$l=1;
		for($i = 0; $i < $arrLength; $i++) 
			{
								
				
				$sql2 = "SELECT Description FROM stor WHERE RefID=?";
				$stmt = $conn->prepare($sql2); 
				$stmt->bind_param("i", $choices[$i]);
				$stmt->execute();
				$result2 = $stmt->get_result();
				 while($row2 = $result2->fetch_assoc()) 
				{
				if($row2["Description"]!="")
				{
				 echo "<tr><td><a href=reader.php?ID=".$choices[$i]."> " . $l  . ": " ;
				 echo htmlentities($row2["Description"]);
				 echo "</a></td></tr> ";
				 $l=l+1;
			    }
			    }

			}
			
		}
		echo "</table>";
	 }
  } 
  else 
  {
  echo "Invalid ID";
  }
  }
  else
  {
	  if($ShowUser=='')
	  {
	  echo '<p style="font-size:32px; ">Available Stories: </p>';
		$sql = "SELECT ID, Title, Opener, Description, Owner FROM Stories ORDER BY rand()";
		$stmt = $conn->prepare($sql); 
		$stmt->execute();
		$result = $stmt->get_result();
		echo "<br>";
		if ($result->num_rows > 0) 
		  {
			    while($row = $result->fetch_assoc()) 
				{
					echo "<br><a href=reader.php?ID=".$row["Opener"].">".htmlentities($row["Title"])."</a> <br> <i>".htmlentities($row["Description"])."</i>"  ;
					echo "<br>";
				}
		  }
	  }
	  else
	  {
		  
	  }
  }
?> 
</html>
